﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tateeda.ClinicalStudy.Base.Modules.Infrastructure.Interfaces.Services;
using Tateeda.ClinicalStudy.Base.Modules.Models.Utility;
using Tateeda.ClinicalStudy.Base.Web.Common;

namespace Tateeda.ClinicalStudy.Base.Web.Areas.Utility.Controllers {

    public class CTSController : Controller {
        private readonly IUtilityService _service;
        private readonly IStudyService _studyService;
        private readonly ISubjectService _subjectService;

        public CTSController(IUtilityService service, IStudyService studyService, ISubjectService subjectService) {
            _service = service;
            _studyService = studyService;
            _subjectService = subjectService;
        }
        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult Index() {
            return View();
        }
        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult CTSubmissions() {
            return View(_service.GetCtGovSubmissions());
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult CTSCreate() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        [HttpPost]
        public ActionResult CTSCreate(CTGovSubmissionModel model) {
            var appUser = _subjectService.GetAppUserByLoginName(User.Identity.Name);
            if (string.IsNullOrWhiteSpace(model.Title)) model.Title = "Update New Submission as of " + DateTime.UtcNow;
            _service.CreateCtGovSubmission(appUser.AppUserId, model.Title, model);
            return RedirectToAction("CTSubmissions");
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult CTSEdit(int id) {
            var submissingXml = _service.GetCtGovSubmission(id);
            return View(_service.SubmissionXmlToModel(submissingXml, id));
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        [HttpPost]
        public ActionResult CTSEdit(CTGovSubmissionModel submission) {
            var appUser = _subjectService.GetAppUserByLoginName(User.Identity.Name);
            var submissionXml = _service.SumissionXmlFromModel(submission);
            _service.UpdateCtGovSubmission(appUser.AppUserId, submission.CTGovSubmissionId, submissionXml, true);

            return RedirectToAction("CTSubmissions");
        }

        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        [ValidateInput(false)]
        public ActionResult CTSDetails(int id) {
            var submissingXml = _service.GetCtGovSubmission(id);
            MvcHtmlString xml = new MvcHtmlString(submissingXml);
            return View(xml);
        }
        [ValidateAntiForgeryToken]
        [Authorize(Roles = Constants.RoleAdmin)]
        public ActionResult CTSDelete(int id) {
            var appUser = _studyService.GetAppUserByName(HttpContext.User.Identity.Name);
            if (appUser != null) {
                _service.DeleteCtGovSubmission(appUser.AppUserId, id);
            }
            return RedirectToAction("CTSubmissions");
        }
    }
}